<template>
  <a-modal v-model:open="open" :width="640"
           :footer="null"
           :title="'当前修改密码的用户是【'+formState.username+'】'"
           :keyboard="false"
           :mask-closable="false"
           @cancel="handleClose">
    <a-form
        :model="formState"
        name="basic"
        class="mt20"
        layout="horizontal"
        style="width: 100%;"
        ref="formRef"
        :rules="rules"
        :label-col=" { style: { width: '150px' } }"
        :wrapper-col="{ span: 24}"
        autocomplete="off"
    >
      <a-row class="flex justify-center">
        <a-col :span="18">
          <a-form-item
              label="输入修改的密码"
              name="password"
          >
            <a-input maxlength="16"  v-model:value="formState.password" placeholder="请输入密码"/>
          </a-form-item>
          <a-form-item>
            <a-button style="margin-left: 150px" type="primary" @click.stop.prevent="onSubmit">修改密码</a-button>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>
<script setup>
const open = ref(false);
const formRef = ref({})
const emits = defineEmits(["success"])
const formState = ref({
  id: "",
  username:"",
  password: '',
});

// 验证规则
const rules = {
  password: [
    {required: true, message: '请输入修改的密码', trigger: 'change'},
    {min: 3, max: 16, message: '长度3到16位', trigger: 'blur'},
  ]
};

const onSubmit = async () => {
  try {
    await formRef.value.validate()
    useHandleUpdatePasswordUserApi(formState.value)
    useMessage().success("修改成功")
    // 窗口关闭
    handleClose()
    // 驱动父类
    emits("success")
  } catch (e) {
    useMessage().error(e.msg)
  }
};


const handleOpen = async (user) => {
  open.value = true
  formState.value.id = user.id
  formState.value.username = user.username
}

const handleClose = () => {
  formState.value = {
    id:"",
    username:"",
    password:""
  }
  open.value = false
}

defineExpose({
  handleOpen
})
</script>

<style lang="scss" scoped>
.full-modal {
  .ant-modal {
    max-width: 100%;
    top: 0;
    padding-bottom: 0;
    margin: 0;
  }

  .ant-modal-content {
    display: flex;
    flex-direction: column;
    height: calc(100vh);
    overflow: auto;
    border-radius: 0px;
  }

  :where(.css-dev-only-do-not-override-1hsjdkk).ant-modal .ant-modal-header {
    background: transparent;
  }

  .ant-modal-body {
    flex: 1;
  }
}
</style>